package com.linkin.homepage.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class AttachView extends View {

    private ViewTreeObserver.OnWindowAttachListener mWindowListener;

    public AttachView(Context context) {
        super(context);
    }

    public AttachView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AttachView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AttachView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setWindowAttachListener(ViewTreeObserver.OnWindowAttachListener listener) {
        mWindowListener = listener;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mWindowListener != null) {
            mWindowListener.onWindowAttached();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mWindowListener != null) {
            mWindowListener.onWindowDetached();
        }
    }
}
